var getCookie = readCookie("user")
var arr=new Array()
var myIsId=""
$(function(){
	if(getCookie!=""){
		getVue1()
		initData()
		
	}else{
	
		location.href="login.htm"
	}
	
})
function getVue1(){
	vm_1=new Vue({
		el:".Shopping_list",
		data:{
			info:[]
		},
		methods:{
			deleteShop(id){
				myIsId=id
				 $.DialogByZ.Confirm({Title: "", Content: "是否删除",FunL:confirmL})
				
			},
			updatenum(id,cid,price){
				if(id=="add"){
					addNum(cid,price)
				}else if(id="del",price){
					delNum(cid,price)	
				}
			},
			checkBoxItem(){
			$(".sumPrice").html("")
			checkBoxItem()
			},
			checkAll(){
				var flag=$("#CheckedAll").prop("checked")
				$("input[type='checkbox']").prop("checked",function(){
					this.checked=flag
				})
				checkBoxItem()
			},
			collct(id){
				collct(id)
			},
			buyCommodity(){
				buyCommodity()
			}
		
		}
	})
}
 function confirmL(){
			$.DialogByZ.Close();
			deleteShop(myIsId)
   }
function buyCommodity(){
	var cinfo=vm_1.$data.info
	
	$("input[type='checkbox']").prop("checked",function(){
		
	 if(this.checked==true){
		 
		 for(var i=0;i<cinfo.length;i++){
			
			 if(this.checked==true&&cinfo[i].id==this.value){
				 
				 if(getCommodity(cinfo[i].cId)<cinfo[i].cNum){
				 					 this.checked=false
									 
				 }else{
					 arr.push(cinfo[i].id)
					
				 }
				 
			 }
		 }
		 
	 }
	});
	if(arr.length>0){
		location.href="Orders.html?cIds="+arr
	}
}
function collct(id){
	
		var clist=""
		$.ajax({
			url:"http://localhost:8080/commodity/chooseCommodity",
			data:{
				"id":id
			},
			type:"post",
			dataType:"json",
			async:false,
			success:function(data){
				var flag=userSelect(id)
				
				if(data.data.length>0&&flag==0){
					addCollect(id)
				}else if(data.data.length==0){
					$.DialogByZ.Alert({Title: "提示", Content: "商品已被下架",BtnL:"确定"})
				}else if(flag>0){
						$.DialogByZ.Alert({Title: "提示", Content: "商品已被添加过",BtnL:"确定"})
				}
				
			}
		})
	
	
}
function userSelect(cId){
	var flag=0
	$.ajax({
		url:"http://localhost:8080/collect/userSelect",
		data:{
			"cId":cId,
			"uId":getCookie
		},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			
			flag=data.data
			
		}
	})
	return flag
}
function addCollect(cId){
	$.ajax({
		url:"http://localhost:8080/collect/add",
		data:{
			"cId":cId,
			"uId":getCookie
		},
		type:"post",
		dataType:"json",
		
		success:function(data){
			
			if(data.status==1){
				$.DialogByZ.Autofade({Content: "收藏完成"})
			}else{
					$.DialogByZ.Alert({Title: "提示", Content: data.msg,BtnL:"确定"})
			}
			
		}
	})
}
function checkBoxItem(){
	var sPrice=0
	
	var cinfo=vm_1.$data.info
	
	$("input[type='checkbox']").prop("checked",function(){
		
	 if(this.checked==true){
		 
		 for(var i=0;i<cinfo.length;i++){
			
			 if(this.checked==true&&cinfo[i].id==this.value){
				 
				 if(getCommodity(cinfo[i].cId)<cinfo[i].cNum){
				 					 this.checked=false
									$.DialogByZ.Alert({Title: "提示", Content: "库存不足",BtnL:"确定"})
				 }else{
					 sPrice=parseFloat(sPrice)+parseFloat(cinfo[i].cPrice)*parseFloat(cinfo[i].cNum)
					
				 }
				 
			 }
		 }
		 
	 }
	});
	 $(".sumPrice").html(sPrice+"元")
	}
function addNum(id,price){
	var count=getCommodity(id)
	var number=$("input[cid='cid"+id+"']").val()
	if(parseInt(count)>parseInt(number)){
		$.ajax({
			url:"http://localhost:8080/shopcar/addNum",
			data:{
				"cId":id,
				"uId":getCookie,
				"cNum":"1",
				"sPrice":price
			},
			type:"post",
			dataType:"json",
			
			success:function(data){
				
				initData()
				checkBoxItem()
			}
		})
	}else{
		$.DialogByZ.Alert({Title: "提示", Content: "库存不足",BtnL:"确定"})
		
	}
}

function delNum(id,price){
	var number=$("input[cid='cid"+id+"']").val()
	if(number>1){
		
	$.ajax({
		url:"http://localhost:8080/shopcar/cutNum",
		data:{
			"cId":id,
			"uId":getCookie,
			"cNum":"1",
			"sPrice":price
		},
		type:"post",
		dataType:"json",
		
		success:function(data){
			
			initData()
			checkBoxItem()
		}
		})
	}
}
function getCommodity(cid){
	var clist=""
	$.ajax({
		url:"http://localhost:8080/commodity/chooseCommodity",
		data:{
			"id":cid
		},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			
			clist=data.data[0].stock
			
		}
	})
	return clist
}
function deleteShop(id){
	$.ajax({
		url:"http://localhost:8080/shopcar/delete",
		data:{
			"id":id
		},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
		if(data.status==1){
			initData()
			getUcount()
		}
			
			
		}
	})
}
function initData(){
	$.ajax({
		url:"http://localhost:8080/shopcar/select",
		data:{
			"uId":getCookie
		},
		type:"post",
		dataType:"json",
		
		success:function(data){
	
			vm_1.$data.info=data.data.list
			
		}
	})
}
